package com.example.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;

public class BatteryLevelReceiver extends BroadcastReceiver {
    private static final String TAG ="BatteryLevelReceiver";
    @Override
    public void onReceive(Context context, Intent batteryStatus) {
        int status=batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
//判断电池电量
        //剩余电量
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        //电量最大值
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        //电量百分比
        float batteryPct = level / (float)scale;
        if (batteryPct<=0.2){
            Log.d(TAG,"当前电量低"+batteryPct);
        }else{
            Log.d(TAG,"当前电量充足"+batteryPct);
        }
    }
}
